package com.ly.controller;

import com.ly.bean.Menu;
import com.ly.service.AdminService;
import com.ly.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李岩
 * @date 2021/12/27-14:21
 * @describe
 */
@Controller
@RequestMapping("/menu/")
public class MenuController {
    private final Logger logger= LoggerFactory.getLogger(MenuController.class);

    @Reference(url = "dubbo://localhost:20886")
    private MenuService menuService;

    private MenuController(){
        System.out.println("进入了 MenuController  controller层");
    }

    //调转到menu_add.html页面
    @RequestMapping("toAddMenu")
    public String toAddMenu(Model model){
        try {
            logger.info("调转到menu_add.html页面 ");
            //调转到menu_add.html页面  但是要先获取到一级菜单的集合
            Map<String, Object> map = new HashMap<>();
            List<Menu> firstMenuList = menuService.findMenuByMap(map);
            model.addAttribute("firstMenuList",firstMenuList);
            return "menu_add";
        } catch (Exception e) {
            logger.error("调转到menu_add.html页面 出现异常 "+e);
        }
        return "error";
    }
    //添加菜单
    @RequestMapping("addMenu")
    public String addMenu(Menu menu,Model model){
        try {
            logger.info("添加菜单 "+menu);
            menuService.insertSelective(menu);
            return "redirect:/role/manager";
        } catch (Exception e) {
            logger.error("添加菜单 出现异常 "+e);
        }
        return "error";
    }
}
